package hasthi.tools;

import hasthi.common.LogFile;
import hasthi.common.Utils;
import hasthi.instrument.HostAgent;
import hasthi.metamodel.resource.ManagedResource.ResourceType;
import hasthi.tools.analyze.TestManager;

import java.util.Random;

import javax.xml.namespace.QName;

import org.apache.log4j.LogManager;

public class DummyServiceSet {
    private static int createdCount = 0;
    private static float failureProb = 0.01f;
    public static void createAService(String serviceLocalPart,ResourceType category)throws Exception{
        try {
            createdCount++;
            DummyService dummyService = new DummyService();
            QName serviceType = new QName("http://extreme.indiana.edu/dummyservice/",serviceLocalPart);
            String name = serviceType.getLocalPart() + "_Set_"+Utils.getLocalHost() + System.currentTimeMillis() + Utils.randomInt(10000);
            QName serviceName = new QName(serviceType.getNamespaceURI(),
                    serviceType.getLocalPart() + name.hashCode());
            dummyService.start(0, serviceName,serviceType,category,failureProb,Utils.getLocalHost());
            if(createdCount%50 == 0){
                System.out.println(createdCount + "services created");
            }
            Thread.sleep(1000);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
    
    
    
    public static void main(String[] args) throws Exception {
        Thread t = new Thread(new Runnable() {
            public void run() {
                try {
                    
                    //this will die five seconds before the managers
                    Thread.sleep(TestManager.TEST_ROUND_TIME -60*1000);
                    System.out.println("Shutting down Service Set at "+ Utils.getLocalHost());
                    try {
                        LogFile.flushAll();
                        LogManager.shutdown();
                    } catch (RuntimeException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.exit(0);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        
        });
        t.start();
        
        int serviceCount = Integer.parseInt(args[0]);
//        if(args.length > 1){
//            failureProb = Float.parseFloat(args[1]);    
//        }
        
        HostAgent.main(new String[]{});
        createdCount++;
        
        Random r = new Random();
        
        for(String name:TestingUtils.PERSISTANT_SERVIES){
            if(createdCount < serviceCount){
                createAService(name,ResourceType.Service);    
            }
        }
        while(createdCount < serviceCount){
            int i = r.nextInt(TestingUtils.APP_SERVICES.length);
            createAService(TestingUtils.APP_SERVICES[i],ResourceType.TransientService);   
        }
    }
}
